import request from '@/utils/request'
import axios from 'axios';

import { STORAGE_BASE_URL, VERSION } from '@/api/common'


/*创建新闻*/
export function createNews(data = {}) {
  return request({
    url: `/v${VERSION}/news`,
    method: 'post',
    data
  })
}

/*根据条件筛选新闻*/
export function getNews(params = {}) {
  return request({
    url: `/v${VERSION}/news`,
    method: 'get',
    params
  })
}

/*新闻详情*/
export function newsDetail(newsId) {
  return request({
    url: `/v${VERSION}/news/${newsId}`,
    method: 'get',
  })
}

/*新闻内容编辑*/
export function editNewsContent(newsId, data) {
  return request({
    url: `/v${VERSION}/news/content/${newsId}`,
    method: 'patch',
    data
  })
}

/*新闻相关信息*/
export function editNewsInfo(newsId, data) {
  return request({
    url: `/v${VERSION}/news/${newsId}`,
    method: 'patch',
    data
  })
}

export function editNews({ newsId, typeid, top, title, mdContent, htmlContent }) {
  return axios.all(
    [
      editNewsContent(newsId, { mdContent, htmlContent }),
      editNewsInfo(newsId, { typeid, title, top })
    ]
  ).then(axios.spread((acct, perms) => {
    console.log(acct, perms)
  }));
}


/*获取所有类型*/
export function getNewsTypes() {
  return request({
    url: `/v${VERSION}/newstypes`,
    method: 'get',
  })
}


/*修改新闻类别信息*/
export function editNewsTypeInfo(typeid, data) {
  return request({
    url: `/v${VERSION}/newstypes/${typeid}`,
    method: 'patch',
    data
  })
}

/*创建新闻类别信息*/
export function createTypes(data) {
  return request({
    url: `/v${VERSION}/newstypes`,
    method: 'post',
    data
  })
}


/*获取云存储的具体内容*/
export function newsContent(storageKey) {
  return request({
    url: `${STORAGE_BASE_URL}${storageKey}`,
    method: 'get',
  })
}





//请求一级分类数据
export function getType() {
  return request({
    url: '/getType',
    method: 'get'
  })
}

//请求二级分类数据
export function getNextType(data) {
  return request({
    url: '/getNextType',
    method: 'post',
    data
  })
}

//请求三级数据
export function getLastType(data) {
  return request({
    url: '/getLastType',
    method: 'post',
    data
  })
}

//测试
export function getTMS() {
  return request({
    url: '/getTMS',
    method: 'get',
  })
}

export function getShop(data) {
  return request({
    url: '/shop/getShopList',
    method: 'post',
    data
  })
}

